{extend name="../template/admin/head.html"}
{block name="title"}
用户设置_{__block__}
{/block}
{block name="csssrc"}
<link rel="stylesheet" href="__adminstatic__/layui/css/user.css">
{/block}
{block name="content"}
<blockquote class="layui-elem-quote title" style="margin-left:10px;margin-top:10px;"><h2>用户设置</h2></blockquote>
<form class="layui-form" lay-filter="user">
  <div class="user_left">
    <div class="layui-form-item">
      <label class="layui-form-label" name="username">登录名</label>
      <div class="layui-input-block">
        <input type="text" id="username" value="{$user['username']}" class="layui-input" />
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">用户昵称</label>
      <div class="layui-input-block">
        <input type="text" value="" placeholder="用户名" lay-verify="required" class="layui-input" name="nickname" id="nickname">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">邮箱</label>
      <div class="layui-input-block">
        <input type="text" value="" placeholder="请输入邮箱" lay-verify="required|email" class="layui-input" name="email" id="email">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">签名</label>
      <div class="layui-input-block">
        <textarea placeholder="请输入内容" class="layui-textarea" name="autograph" id="autograph"></textarea>
      </div>
    </div>

  </div> 
  <div class="user_right">
    <button type="button" class="layui-btn" id="faceupload" style="margin-bottom: 5%;">
      <i class="layui-icon">&#xe67c;</i>上传头像(只允许png格式)
    </button>
    <img src="__indexstatic__/images/face.png" class="layui-circle" id="userFace">
  </div>

  <div class="layui-form-item" style="margin-left: 5%;">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit="submit" lay-filter="changeUser">立即提交</button>
      <div class="layui-btn-group">
        <a class="layui-btn" id="xgpwd">修改密码</a>
      </div>

    </div>
  </div>
</form>
</div>
{/block}
{block name="jssrc"}
<script>

  layui.use(['form', 'upload'], function(){
    var form = layui.form;
    var upload = layui.upload;
  //执行实例
  var uploadInst = upload.render({
    elem: '#faceupload' //绑定元素
    ,url: "{:URL('User/faceupload')}" //上传接口
    ,exts: 'png'
    ,done: function(res){
      if (res.code=='0') {
        layer.msg('上传成功');
        setTimeout(function(){
        location.reload();
        },1000);
      }
    }
    ,error: function(){
      if (res = null) {
        layer.msg('接口异常');
      }
    }
  });
  form.val("user", {
    "nickname": "{$user['nickname']}",
    "email": "{$user['email']}",
    "autograph": "{$user['autograph']}"
  });
});

  $('#xgpwd').on('click', function(){
    var content='<form class="layui-form" style=" padding-right:20px;">';
    content+='<div style="margin:0 0 15px 110px;color:#f00;">';
    content+='新密码必须两次输入一致才能提交';
    content+='</div>';
    content+='<div class="layui-form-item">';
    content+='<label class="layui-form-label">旧密码</label>';
    content+='<div class="layui-input-block">';
    content+='<input type="password" value="" placeholder="请输入旧密码" lay-verify="required|oldPwd" class="layui-input pwd" id="oldpwd" />';
    content+='</div>';
    content+='</div>';
    content+='<div class="layui-form-item">';
    content+='<label class="layui-form-label">新密码</label>';
    content+='<div class="layui-input-block">';
    content+='<input type="password" value="" placeholder="请输入新密码" lay-verify="required|newPwd" class="layui-input pwd" id="newpwd" />';
    content+='</div>';
    content+='</div>';
    content+='<div class="layui-form-item">';
    content+='<label class="layui-form-label">确认密码</label>';
    content+='<div class="layui-input-block">';
    content+='<input type="password" value="" placeholder="请确认密码" lay-verify="required|confirmPwd" class="layui-input pwd" id="truepwd" />';
    content+='</div>';
    content+='</div>';
    content+='<div class="layui-form-item">';
    content+='<div class="layui-input-block">';
    content+='<button class="layui-btn" lay-submit="submit" lay-filter="changePwd">立即修改</button>';
    content+='<button type="reset" class="layui-btn layui-btn-primary">重置</button>';
    content+='</div>';
    content+='</div>';
    content+='</form>';
    layer.open({
      title:'修改密码',
      type: 1,
      area: ['500px', '300px'],
      shadeClose: true, //点击遮罩关闭
      content: content

    });
  });   
//基本信息
layui.use('form', function(){
  var form = layui.form;  
  form.on('submit(changeUser)', function(data) {
    var username = $('#username').val();
    var nickname = $('#nickname').val();
    var email = $('#email').val();
    var autograph = $('#autograph').val();
    if (nickname != "" && email != "" && autograph != "") {
      $.ajax({
        type:"POST",
        url:"{:URL('User/useredit')}",
        dataType:"json",
        data:{nickname:nickname,email:email,autograph:autograph,username:username},
        beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#changeUser").attr({"disabled":"disabled"});
            $("#changeUser").addClass("layui-btn-disabled");
            $("#changeUser").html("修改中...");
            layer.load(1);
          },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (XmlHttpRequest.responseText != "") {
              layer.msg('提交失败，请稍后尝试！');
              $("#changeUser").removeClass("layui-btn-disabled");
              $("#changeUser").removeAttr("disabled");  
              $("#changeUser").html("修改");
            }
          },
          success:function(data){
            //1为成功
            //2为失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#changeUser").removeClass("layui-btn-disabled");
              $("#changeUser").removeAttr("disabled");  
              $("#changeUser").html("修改");
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#changeUser").removeClass("layui-btn-disabled");
              $("#changeUser").removeAttr("disabled");  
              $("#changeUser").html("修改");

            }
          }
        });
      return false;
    }else{
      layer.msg('所有内容不能为空!');
      return false;
    }
  });
});
layui.use('form', function(){
  var form = layui.form;  
  form.on('submit(changePwd)', function(data) {
    var oldpwd = $('#oldpwd').val();
    var newpwd = $('#newpwd').val();
    var truepwd = $('#truepwd').val();
    if (newpwd != truepwd) {
      layer.msg('两次密码输入不一致!'+newpwd+truepwd);
      return false;
    }
    if (oldpwd != "" && newpwd != "") {
      $.ajax({
        type:"POST",
        url:"{:URL('User/userpwd')}",
        dataType:"json",
        data:{oldpwd:oldpwd,newpwd:newpwd},
        beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#changePwd").attr({"disabled":"disabled"});
            $("#changePwd").addClass("layui-btn-disabled");
            $("#changePwd").html("修改中...");
            layer.load(1);
          },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (XmlHttpRequest.responseText != "") {
              layer.msg('提交失败，请稍后尝试！');
              $("#changePwd").removeClass("layui-btn-disabled");
              $("#changePwd").removeAttr("disabled");  
              $("#changePwd").html("修改");
            }
          },
          success:function(data){
            //1为添加成功
            //2为添加失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#changePwd").removeClass("layui-btn-disabled");
              $("#changePwd").removeAttr("disabled");  
              $("#changePwd").html("修改");
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#changePwd").removeClass("layui-btn-disabled");
              $("#changePwd").removeAttr("disabled");  
              $("#changePwd").html("修改");

            }
          }
        });
      return false;
    }else{
      layer.msg('所有内容不能为空!');
      return false;
    }
  });
});
</script>
{/block}